package com.softqin.washer.activity;

import java.util.List;

import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ab.activity.AbActivity;
import com.softqin.washer.R;
import com.softqin.washer.bean.AddressBean;
import com.softqin.washer.common.WasherApp;
import com.softqin.washer.db.WashDbManager;
import com.softqin.washer.utils.LogUtils;
import com.softqin.washer.utils.UiUtils;
/**
 * 
* @ClassName: MainActivity 

 */
public class MainActivity extends AbActivity {

	private String TAG = MainActivity.class.getSimpleName();
	private boolean isExitApp;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setAbContentView(R.layout.activity_main);
		setTiTleBar("南京",getString(R.string.app_name));
		WasherApp.getInstance().addActivity(this);
		
		AddressBean addressBean = new AddressBean();
		addressBean.setTokenid("0001");
		addressBean.setAddress("江苏省南京市雨花台区软件大道50号中兴通讯");
		addressBean.setdTokenid("00001");
		LogUtils.d(TAG, "result " + WashDbManager.getInstance(MainActivity.this).addAddress(addressBean));
		
		List<AddressBean> lisAddressBeans = WashDbManager.getInstance(MainActivity.this).findAllAddress();
		if (null != lisAddressBeans && lisAddressBeans.size() > 0) {
			AddressBean tempAddressBean = lisAddressBeans.get(0);
			LogUtils.d(TAG, tempAddressBean.getAddress());
		}
		
		LogUtils.d(TAG, "onCreate");
	}
	
	/**
	 */
	public void setTiTleBar(final String city,String name)
	{
	    this.setTitleLayoutBackground(R.drawable.bg_titlebar);
	    this.setTitleText(name);
        
        this.setTitleTextMargin(20,0,0, 0);
        if(city != null && !city.equals("")) {
        	this.setLogo(new BitmapDrawable(UiUtils.getTextBitMap(city,66)));
	        this.setLogoBackOnClickListener(new View.OnClickListener(){
	            @Override
				public void onClick(View v)
	            {
	                Toast.makeText(getApplicationContext(),"当前城市是"+city,1000).show();
	            }});
	        initTitleRightLayout();
        } else {
        	this.setLogo(null);
        }
	}
	
	/**
	 */
	private void initTitleRightLayout()
    {		
        this.clearRightView();
        View rightViewApp = mInflater.inflate(R.layout.app_btn, null);
        addRightView(rightViewApp);
        ImageButton appBtn = (ImageButton) rightViewApp.findViewById(R.id.btnRefresh);
        appBtn.setVisibility(View.INVISIBLE);

        appBtn.setOnClickListener(new View.OnClickListener() {
            @Override
			public void onClick(View v) {
            	//this should write 
            }});
        this.setTitleLayoutGravity(Gravity.CENTER, Gravity.CENTER);
    }
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	private Handler handlerExit = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			isExitApp = false;
		}
	};

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (!isExitApp) {
				isExitApp = true;
				Toast.makeText(MainActivity.this,R.string.exitApp, 1000).show();
				handlerExit.sendEmptyMessageDelayed(0, 2000);
			} else {
				WasherApp.getInstance().finishAllActivity();
			}
		}
		return false;
	}
	
	

}
